package doantotnghiep.business;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import doantotnghiep.dao.MedicamentBatchDao;
import doantotnghiep.model.MedicamentBatch;

public class MedicamentBatchBusiness {
	
	@Autowired
	private MedicamentBatchDao batchDao;

	public List<MedicamentBatch> listBatch() {
		return batchDao.getListBatch();
	}

	public MedicamentBatch getBatchByID(int id) {
		return batchDao.getBatchByID(id);
	}

	public void saveChanged(MedicamentBatch batch) {
		batchDao.saveChanged(batch);
		
	}

	public void deleteBatch(int id) {
		batchDao.deleteBatch(id);
		
	}

	public void addBatch(MedicamentBatch batch) {
		batchDao.addBatch(batch);
		
	}

	public List<MedicamentBatch> getListBatchByExpirationDate() {
		String candidateDate = convertString(60).toString();
		System.out.println("sau khi tang len:"+candidateDate);
		List<MedicamentBatch> list = batchDao.getListBatchByExpirationDate(candidateDate);
		System.out.println("size: "+list.size());
	    return list;	 
	}
	 public StringBuilder convertString(int limitDays){
		 DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
		 String currentDate = df.format(new Date()).toString(); 
		 System.out.println("ngay hien tai: "+currentDate);
		 int date[] = new int[3];
		 String tempd = "";
		 tempd += currentDate.charAt(0);
		 tempd += currentDate.charAt(1);
		 int day = Integer.parseInt(tempd);
		 date[0] = day;//month
		 System.out.println("thang; "+date[0]);
		 tempd = "";
		/*====================================*/
		 tempd += currentDate.charAt(3);
		 tempd += currentDate.charAt(4);
		 day = Integer.parseInt(tempd);
		 date[1]= day;
		 System.out.println("ngay; "+date[1]);
		 tempd = "";//day
		 /*====================================*/
		 tempd += currentDate.charAt(6);
		 tempd += currentDate.charAt(7);
		 tempd += currentDate.charAt(8);
		 tempd += currentDate.charAt(9);
		 day  = Integer.parseInt(tempd);
		 date[2] = day;//year
		 System.out.println("nam; "+date[2]);
		 int i = 0;
		 while(i<limitDays){
			 date[1]++;
			 if(date[1]>getDayOfMonth(date[0])){
				 date[0]++;
				 date[1] = 1;
				 if(date[0]>12){
					 date[2]++;
					 date[0] = 1;
				 }
			 }	 
		 }
		 StringBuilder result = new StringBuilder();
		 for( i = 0 ; i < 3 ; i++ ){
			result.append(Integer.toString(date[i]));
			if(i!=2){
				result.append("/");
			}
		System.out.println("result: "+result);
		 }
		 return result;
	}
	 public int getDayOfMonth(int month){
		 int date[] ={0,31,28,31,30,31,30,31,31,30,31,30,31};
		 return date[month];
	 }
		


}
